//
//  LeftMenuViewController.swift
//  iiatn
//
//  Created by mnine on 18/4/15.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD
import SwiftKeychainWrapper
import Alamofire
import SwiftyJSON

class LeftMenuViewController: itBaseViewController {
    
    fileprivate let cellIdentifier = "WLCellIdentifier"

    let headerViewH: CGFloat = 220
    
    var dataArray = [[String]]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        dataArray = [["个人信息","info_black_18x18"],["关于我们","about_black_18x18"],["留言反馈","edit_black_18x18"]]
        view.addSubview(headerView)
        view.addSubview(tableView)
        
        let url = BASE_URL + "apin/version"
        Alamofire
            .request(url)
            .responseJSON { (response) in
                guard response.result.isSuccess else {
                    //SVProgressHUD.showError(withStatus: "网络连接失败...")
                    return
                }
                if let value = response.result.value {
                    let dict = JSON(value)
                    let status = dict["status"].intValue
                    let off = dict["off"].intValue
                    if status == RETURN_OK {
                        if off == 0 {
                            self.dataArray.append(["分享APP","share_black_18x18"])
                            self.dataArray.append(["合作伙伴","friend_black_18x18"])
                            //self.dataArray.append(["退出","out_black_18x18"])
                            self.tableView.reloadData()
                        }
                    }
                }
        }
    }
    
    private lazy var tableView: UITableView = {
        
        let tab = UITableView(frame: CGRect(x: 0, y: self.headerViewH, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - self.headerViewH), style: .plain)
        //tab 背景颜色
        tab.backgroundColor = WColor(r: 245, g: 245, b: 245, a: 0.8)
        tab.separatorStyle = UITableViewCellSeparatorStyle.singleLineEtched
        tab.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        tab.delegate = self
        tab.dataSource = self
        tab.register(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
        return tab
    }()
    
    
    private lazy var headerView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: self.headerViewH))
        let bgImageView = UIImageView(frame: view.frame)
        bgImageView.image = UIImage(named: "banner_")
        bgImageView.contentMode = UIViewContentMode.scaleAspectFill
        bgImageView.clipsToBounds = true
        view.addSubview(bgImageView)
        return view
        
    }()
    
}

extension LeftMenuViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
        cell.backgroundColor = UIColor.clear
        cell.textLabel?.text = dataArray[indexPath.row][0]
        cell.imageView?.image = UIImage(named: dataArray[indexPath.row][1])
        cell.textLabel?.textColor = UIColor.black
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print(indexPath.row)
        switch indexPath.row {
        case 4:
            let vc = WHZViewController()
            vc.view.backgroundColor = UIColor.white
            vc.title = dataArray[indexPath.row][0]
            DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
        case 1:
            let vc = WAUViewController()
            vc.title = dataArray[indexPath.row][0]
            DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
        case 2:
            let vc = WLYViewController()
            vc.title = dataArray[indexPath.row][0]
            DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
        case 3:
            let vc = WSAViewController()
            vc.title = dataArray[indexPath.row][0]
            DrawerViewController.shareDrawer?.LeftViewController(didSelectController: vc)
        case 5:
            tuichu()
        default:
            if whaslogin! {
                WNetwork.shareNetworkTool.loadPerson(user_id: wuserid!){ (mmsg) in
                    self.info()
                }
            }else {
                SVProgressHUD.showInfo(withStatus: "对不起，您还没有登录！")
            }
        }
    }
    func info(){
        var msg = "\n\n用户： \(wname!)\n"
        msg += "邮箱： " + wemail! + "\n"
        msg += "公司： " + wcompany_name! + "\n"
        msg += "省级： " + wprovince! + "\n"
        msg += "市级： " + wcity!
        let alertController = UIAlertController(title: "👤 个人信息",
                                                message: msg, preferredStyle: .alert)
        //let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "确认", style: .default, handler: {
            action in
            //
        })
        //alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        
        let paragraphStyle = NSMutableParagraphStyle() // 注意一定要是mutable!
        paragraphStyle.alignment = NSTextAlignment.left // 居左
        let messageText = NSMutableAttributedString(
            string: msg,
            attributes: [
                NSParagraphStyleAttributeName: paragraphStyle,
                NSFontAttributeName : UIFont.systemFont(ofSize: 14)//, // 设置一下字号
            ]
        )
        alertController.setValue(messageText, forKey: "attributedMessage") // 通过KVC 进行修改
        let height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)
        alertController.view.addConstraint(height);
        
        self.present(alertController, animated: true, completion: nil)
    }
    func tuichu(){
        let mexit = KeychainWrapper.standard.removeAllKeys()
        print("退出：\(mexit)")
    }
    
}
